package com.air.ota.vo;

import com.air.ota.util.HexStringUtil;
import com.air.ota.util.PBOCUtil;
import com.air.ota.util.PkgUtil;

//开通认证请求命令
public class UTKRegRequestUplinkVO extends UTKBaseUplinkPgkVO {
	
	protected byte[] cardAppSequence;
	protected byte[] cardPasswordEncrypt;//在v1.1.5中添加 服务认证密码
	protected byte[] regSequence;
	protected byte[] cardRandom;
	protected byte[] lastTransData;
	protected byte[] cardDeposit;
	protected byte[] cardRemain;
	protected byte[] cardTransSequence;
	protected byte[] cardSumDeposit;
	protected byte   cardType;
	protected byte[] pkgSequence;
	protected byte   cardMerchant;
	
	protected String cardPasswordDecrypt;
	
	
	public UTKRegRequestUplinkVO(){
		super();
		this.cmdType = 0x04;
		
		this.cardAppSequence  = new byte[8];
		this.regSequence = new byte[2];
		this.cardRandom = new byte[4];
		this.lastTransData = new byte[23];
		this.cardDeposit = new byte[4];
		this.cardRemain = new byte[4];
		this.cardTransSequence = new byte[2];
		this.cardSumDeposit = new byte[4];
		this.pkgSequence = new byte[4];
		
		cardPasswordEncrypt= new byte[8];
	}
	
	
	@Override
	public  void parse(byte[] pkg){
		
		super.parse(pkg);
		
		//System.arraycopy(pkg, 20, this.cmdLength, 0, 2);
		
		//得到加密的命令参数 (由于卡片序列号和卡商代码没有被加密，故对于父类中相关的算法进行了override)
		int encCmdDataLength = pkg.length-23-16-1;//去头23byte，去1byte卡商代码 去卡片序列号8byte,去8byte mac和random8byte
		encCmdData = new byte[encCmdDataLength];
		System.arraycopy(pkg, 23, encCmdData, 0, encCmdDataLength);
		
		
		//获取卡片序列号
		System.arraycopy(pkg, pkg.length-16, super.cardSequence, 0,8);
		
		
		
		//UTKSessionKey = pbocUtil.buildUTKSessionKey(cardSequence, this.random);
		
		
		
		//解密命令参数
		//TODO 用机密机替代
		
		//this.cmdData = pbocUtil.TripleDes(UTKSessionKey, encCmdData, 0);
		
		decryptCmdData();
		
		System.arraycopy(this.cmdData, 0, this.cardAppSequence, 0, 8);
		System.arraycopy(this.cmdData, 8, this.cardPasswordEncrypt, 0, 8);
		System.arraycopy(this.cmdData, 16, this.regSequence, 0, 2);
		System.arraycopy(this.cmdData, 18, this.cardRandom, 0, 4);
		System.arraycopy(this.cmdData, 22, this.lastTransData, 0, 23);
		System.arraycopy(this.cmdData, 45, this.cardDeposit, 0, 4);
		System.arraycopy(this.cmdData, 49, this.cardRemain, 0, 4);
		System.arraycopy(this.cmdData, 53, this.cardTransSequence, 0,2);
		System.arraycopy(this.cmdData, 55, this.cardSumDeposit, 0,4);
		this.cardType = this.cmdData[59];
		System.arraycopy(this.cmdData, 60, this.pkgSequence, 0,4);
		PkgUtil.setCurrentPkgSequence(pkgSequence);
		
		//newPwdDecrypt = HexStringUtil.bytesToHexString(pboc.TripleDes(this.UTKSessionKey, oldPwdEncrypt, 0));
		PBOCUtil pboc = new PBOCUtil();
		cardPasswordDecrypt = HexStringUtil.bytesToHexString(pboc.TripleDes(this.UTKSessionKey, this.cardPasswordEncrypt, 0));
		
		

	}


	public byte[] getCardAppSequence() {
		return cardAppSequence;
	}


	public void setCardAppSequence(byte[] cardAppSequence) {
		this.cardAppSequence = cardAppSequence;
	}


	public byte[] getRegSequence() {
		return regSequence;
	}


	public void setRegSequence(byte[] regSequence) {
		this.regSequence = regSequence;
	}


	public byte[] getCardRandom() {
		return cardRandom;
	}


	public void setCardRandom(byte[] cardRandom) {
		this.cardRandom = cardRandom;
	}


	public byte[] getLastTransData() {
		return lastTransData;
	}


	public void setLastTransData(byte[] lastTransData) {
		this.lastTransData = lastTransData;
	}


	public byte[] getCardDeposit() {
		return cardDeposit;
	}


	public void setCardDeposit(byte[] cardDeposit) {
		this.cardDeposit = cardDeposit;
	}


	public byte[] getCardRemain() {
		return cardRemain;
	}


	public void setCardRemain(byte[] cardRemain) {
		this.cardRemain = cardRemain;
	}


	public byte[] getCardTransSequence() {
		return cardTransSequence;
	}


	public void setCardTransSequence(byte[] cardTransSequence) {
		this.cardTransSequence = cardTransSequence;
	}


	public byte[] getCardSumDeposit() {
		return cardSumDeposit;
	}


	public void setCardSumDeposit(byte[] cardSumDeposit) {
		this.cardSumDeposit = cardSumDeposit;
	}


	public byte getCardType() {
		return cardType;
	}


	public void setCardType(byte cardType) {
		this.cardType = cardType;
	}


	public byte[] getPkgSequence() {
		return pkgSequence;
	}


	public void setPkgSequence(byte[] pkgSequence) {
		this.pkgSequence = pkgSequence;
	}


	public byte getCardMerchant() {
		return cardMerchant;
	}


	public void setCardMerchant(byte cardMerchant) {
		this.cardMerchant = cardMerchant;
	}


	public byte[] getCardSequence() {
		return cardSequence;
	}


	public void setCardSequence(byte[] cardSequence) {
		this.cardSequence = cardSequence;
	}
	
	public String getCardPasswordDecrypt(){
		return this.cardPasswordDecrypt;
	}
	
	public void print(){
		System.out.println("print UTKRegRequestUplink   UTK卡开通认证请求");
		
		System.out.println("原始密文="+HexStringUtil.bytesToHexString(this.encCmdData));
		System.out.println("原始明文="+HexStringUtil.bytesToHexString(this.cmdData));
		System.out.println("随机数="+HexStringUtil.bytesToHexString(this.random));
		System.out.println("卡片序列号="+HexStringUtil.bytesToHexString(this.cardSequence));
		System.out.println("会话密钥="+HexStringUtil.bytesToHexString(this.UTKSessionKey));
		
		System.out.println("命令类型="+HexStringUtil.byteToHexString(this.getCmdType()));
		System.out.println("命令长度="+HexStringUtil.bytesToHexString(this.cmdLength));
		System.out.println("应用序列号="+HexStringUtil.bytesToHexString(this.cardAppSequence));
		System.out.println("password="+this.cardPasswordDecrypt);
		System.out.println("开通流水号="+HexStringUtil.bytesToHexString(this.regSequence));
		System.out.println("随机数1="+HexStringUtil.bytesToHexString(this.cardRandom));
		System.out.println("末笔交易记录="+HexStringUtil.bytesToHexString(this.lastTransData));
		System.out.println("押金="+HexStringUtil.bytesToHexString(this.cardDeposit));
		System.out.println("卡余额="+HexStringUtil.bytesToHexString(this.cardRemain));
		System.out.println("卡交易序号="+HexStringUtil.bytesToHexString(this.cardTransSequence));
		System.out.println("充值累计金额="+HexStringUtil.bytesToHexString(this.cardSumDeposit));
		System.out.println("卡类型="+HexStringUtil.byteToHexString(this.cardType));
		System.out.println("报文流水号="+HexStringUtil.bytesToHexString(this.pkgSequence));
		
		
		System.out.println("print END");
	}
	
}
